package com.wyg.ep_timer.task;

import com.wyg.epcommonbase.common.constant.FinalConstant;
import com.wyg.epcommonbase.common.util.CmdUtil;
import com.wyg.epcommonbase.mail.MailReq;
import com.wyg.ep_timer.config.BaseConstant;
import com.wyg.ep_timer.feign.MailFeign;
import com.wyg.ep_timer.entity.Article;
import com.wyg.ep_timer.mapper.ArticleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * @Author: WangYongGan
 * @Date: 2022/5/17 3:07
 * @Description:
 */
@Configuration
@EnableScheduling
@Slf4j
public class CheckDBTask {
    @Autowired
    MailFeign mailFeign;
    @Autowired
    ArticleMapper articleMapper;
    //任务1：checkDB
    public static boolean running = true;

    @Scheduled(cron = "0 0/10 * * * ?")
    public void test() {
        System.out.println("运行checkDB。。。。。");
    }

    @Scheduled(cron = "0 0/10 * * * ?")
    public void checkDB() {
        if (running && BaseConstant.linux) {
            Article article = articleMapper.selectById("-1");
            if (article == null || !article.getTitle().equals(FinalConstant.check)) {
                MailReq req = new MailReq();
                req.setMail(BaseConstant.QQMail);
                req.setSubject("金融舆情");
                req.setText("邮箱疑似被盗");
                boolean send = mailFeign.send(req);
                while (!send) {
                    log.error("email发送失败::" + req);
                    send = mailFeign.send(req);
                }
                //更换状态，停止检查
                running = false;
            } else {
                //更新数据
                CmdUtil.doCmd("sh /root/script/saveData.sh");
                log.info("check db");
            }
        }
    }
}
